package com.fragrance.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fragrance.model.DeviceMainModel;
import com.fragrance.model.UserDeviceBindModel;
import com.fragrance.model.UserDeviceModel;
import com.fragrance.service.UserDeviceBindService;

public class UserDeviceBindServiceImpl implements UserDeviceBindService {

	// 通过imei号查询设备是否已经被绑定
	public List<UserDeviceBindModel> findBindDevice(String imei) {
		return UserDeviceBindModel.me.findBindDevice(imei);
	}

	// 微信绑定设备
	public Boolean bindCarlaToUser(Map<String, Object> data) {
		return UserDeviceBindModel.me.bindCarlaToUser(data);
	}

	// 查询绑定设备信息
	public UserDeviceBindModel findBindDeviceInfo(Map<String, Object> data) {
		return UserDeviceBindModel.me.findBindDeviceInfo(data);
	}

	// 解绑设备
	public boolean unBindCarla(Map<String, Object> data) {
		return UserDeviceBindModel.me.unBindCarla(data);
	}

	// 查询绑定设备列表
	public List<UserDeviceBindModel> getDeviceBindList(String openid) {
		return UserDeviceBindModel.me.getDeviceBindList(openid);
	}
	
	//设备绑定列表
	public Map<String, Object> deviceBindList(Map<String, Object> params) {

		return UserDeviceBindModel.me.deviceBindList(params);
	}
	
	

	// 获取我的设备列表
	@Override
	public List<Map> getMyDeviceList(String openid) {
		List<UserDeviceBindModel> userDeviceBindModelList = UserDeviceBindModel.me.getDeviceBindList(openid);
		List<Map> result = new ArrayList<Map>();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (userDeviceBindModelList == null || userDeviceBindModelList.size() == 0) {
			return result;
		} else {
			for (int i = 0; i < userDeviceBindModelList.size(); i++) {
				UserDeviceBindModel userDeviceBindModel = userDeviceBindModelList.get(i);
				Map<String, Object> data = new HashMap<String, Object>();
				String wx_open_id = userDeviceBindModel.getStr("wx_open_id");
				String imei = userDeviceBindModel.getStr("imei");
				data.put("openid", wx_open_id);
				data.put("imei", imei);
				List<UserDeviceModel> userDeviceModelList = UserDeviceModel.me.getDeviceWorkList(imei);
				if (userDeviceModelList != null && userDeviceModelList.size() > 0) {
					UserDeviceModel userDeviceModel = userDeviceModelList.get(0);
					data.put("online", true);
					data.put("simNo", userDeviceModel.getStr("sim_no"));
					data.put("dampness", userDeviceModel.getStr("dampness"));
					data.put("temperature", userDeviceModel.getStr("temperature"));
					data.put("gear", userDeviceModel.getStr("gear"));
					Date open_time = userDeviceModel.getDate("open_time");
					if (open_time != null) {
						data.put("openTime", format.format(open_time));
					}
					data.put("location", userDeviceModel.getStr("location"));
				} else {
					data.put("online", false);
					data.put("simNo", "");
					data.put("dampness", "");
					data.put("temperature", "");
					data.put("gear", "");
					data.put("openTime", "");
					data.put("location", "");
				}
				result.add(data);
			}
			return result;
		}
	}

	//后台解除设备绑定
	public Map<String, Object> deviceBindUntie(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return UserDeviceBindModel.me.deviceBindUntie(params);
	}

}
